import vue from "@vitejs/plugin-vue";
import { Plugin } from "vite";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import { dirResolver, DirResolverHelper, AutoGenerateImports } from "vite-auto-import-resolvers";

export default function PluginConfig(env: Record<string, any>) {
    const plugins: Plugin[] = [vue()];

    plugins.push(
        DirResolverHelper(),

        //自动加载相应Api
        AutoImport({
            //自动加载Element-Plus Api
            resolvers: [ElementPlusResolver(), dirResolver({ target: "src/gx" })],

            //自动引入数组中的Api，["vue", "vue-router", "pinia"] 或使用AutoGenerateImports()
            imports: AutoGenerateImports(),

            //指定类型声明文件，为true时在项目根目录创建
            dts: "dts/auto-import-api.d.ts",
        }),

        //按需加载组件
        Components({
            //按需加载Element-Plus组件
            resolvers: [ElementPlusResolver({ importStyle: "sass" })],

            //自动加载的组件目录，默认值为 ["src/components"]
            dirs: ["src/components"],

            //组件名称包含目录，防止同名组件冲突
            directoryAsNamespace: false,

            //指定类型声明文件，为true时在项目根目录创建
            dts: "dts/auto-import-components.d.ts",
        })
    );

    return plugins;
}
